package security.internal;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import kernel.exception.BusinessException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import project.Constants;
import security.Role;
import security.SecUser;

public class SecUserServiceImpl extends HibernateDaoSupport implements SecUserService {

    protected final Log logger = LogFactory.getLog(getClass());

    // private PasswordEncoder passwordEncoder;

    public void addUser(SecUser user) {
        SecUser db = findUserByLoginName(user.getUsername());
        if (null != db) {
            throw new BusinessException("系统用户存在相同用户名！");
        }
        user.setCreateTime(new Date());
        user.setPassword(user.getPassword());
        this.getHibernateTemplate().merge(user);

    }

    @SuppressWarnings("unchecked")
    public SecUser findUserByLoginName(String loginName) {
        List<SecUser> users = this.getHibernateTemplate().find("FROM SecUser WHERE username = ?",
                new Object[] { loginName });
        if (users.size() > 0) {
            return users.get(0);
        }
        return null;
    }
    

    @Override
    public SecUser findValidUserByLoginName(String loginName) {
        SecUser user = findUserByLoginName(loginName);
        if (user == null) {
            return null;
        }
        
        if (!user.isEnabled()) {
            return null;
        }
        
        Set<Role> roles = user.getRoles();
        boolean find = false;
        for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
            Role role = (Role) iterator.next();
            if (role.getRoleName().equals(Constants.SECURITY_ROLE_MEMBER)) {
                find = true;
            }

        }
        if (!find) {
            return null;
        }
        return user;
    }


    // public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
    // this.passwordEncoder = passwordEncoder;
    // }

    @Override
    public void updatePassword(Serializable partyId, String oldpassword, String password) {
        List<SecUser> users = this.getHibernateTemplate().find("FROM SecUser WHERE partyId = ?",
                new Object[] { partyId });

        SecUser user = users.get(0);

        if (user == null) {
            return;
        }
        if (user.getPassword().equals(oldpassword)) {
            user.setPassword(password);
            this.getHibernateTemplate().update(user);
        }
        else {
            throw new BusinessException("原一级密码错误");
        }

    }

    @Override
    public SecUser findUserByPartyId(Serializable partyId) {
        StringBuffer queryString = new StringBuffer(" FROM SecUser where partyId = ?");
        List<SecUser> list = null;
        list = this.getHibernateTemplate().find(queryString.toString(), new Object[] { partyId });

        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void update(SecUser user) {
        this.getHibernateTemplate().update(user);

    }

}
